package com.controller;/*
 *
 *作者：张继成
 *任务：
 *
 *备注：
 *@Date2023/06/11/12:32
 ***/

import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.XuekeEntity;
import com.entity.view.XuekeView;
import com.service.XuekeService;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/xueke")
public class XuekeController {
    @Autowired
    private XuekeService xuekeService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, XuekeEntity xueke,
                  HttpServletRequest request){
        EntityWrapper<XuekeEntity> ew = new EntityWrapper<XuekeEntity>();

        PageUtils page = xuekeService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xueke), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,XuekeEntity xueke,
                  HttpServletRequest request){
        EntityWrapper<XuekeEntity> ew = new EntityWrapper<XuekeEntity>();

        PageUtils page = xuekeService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xueke), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( XuekeEntity xueke){
        EntityWrapper<XuekeEntity> ew = new EntityWrapper<XuekeEntity>();
        ew.allEq(MPUtil.allEQMapPre( xueke, "xueke"));
        return R.ok().put("data", xuekeService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(XuekeEntity xueke){
        EntityWrapper< XuekeEntity> ew = new EntityWrapper< XuekeEntity>();
        ew.allEq(MPUtil.allEQMapPre( xueke, "xueke"));
        XuekeView xuekeView =  xuekeService.selectView(ew);
        return R.ok("查询学科成功").put("data", xuekeView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        XuekeEntity xueke = xuekeService.selectById(id);
        return R.ok().put("data", xueke);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        XuekeEntity xueke = xuekeService.selectById(id);
        return R.ok().put("data", xueke);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody XuekeEntity xueke, HttpServletRequest request){
        xueke.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(xueke);
        xuekeService.insert(xueke);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody XuekeEntity xueke, HttpServletRequest request){
        xueke.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(xueke);
        xuekeService.insert(xueke);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody XuekeEntity xueke, HttpServletRequest request){
        //ValidatorUtils.validateEntity(xueke);
        xuekeService.updateById(xueke);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        xuekeService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
